<template>
  <div class="departments-container">
    <!-- 设置padding -->
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 头部公司信息 -->
        <tree-tools
          :node-data="company"
          :is-root="true"
          @show-add="showAddDialog"
        />
        <hr>

        <!-- 树形控件
          data:指定数据
          props:树形成员配置项
          default-expand-all是否展开子节点
        -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 作用域插槽 -->
          <template #default="{ data }">
            <tree-tools
              :node-data="data"
              @del-depts="getDepartments"
              @show-add="showAddDialog"
              @show-edit="showEditDialog"
            />
          </template>
        </el-tree>
      </el-card>

      <!-- 添加组织架构的组件 弹出框 -->
      <!--
        1.属性绑定要传的数据
        2.通知父组件修改
        3.父组件修改值
       -->
      <!--
        .sync修饰符:作用 简化了在子组件修改父组件传过来值修改操作
        1.属性绑定加.sync
        2.在组件内部修改数据 this.$emit("update:showDialog",false)
      -->

      <!-- 常见的修饰符: prevent stop native sync -->

      <add-dept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :node-data="nodeData"
        :list-data="listData"
        @add-detps="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { reqGetDepartments } from '@/api/department'
import { transListDataToTreeData } from '@/utils'
import hello from '@/mixins/hello'
// transListDataToTreeData(list,'1')
export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  mixins: [hello], // 混入
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {},
      showDialog: false,
      nodeData: {},
      listData: [],
      loading: false
    }
  },
  created() {
    // console.log('今天天气真热,烦死了')
    this.getDepartments()
  },
  methods: {
    // 根据api获取组织架构数据
    async getDepartments() {
      this.loading = true
      // 模拟网络不好
      // await new Promise((resolve, reject) => {
      //   setTimeout(() => {
      //     resolve()
      //   }, 2000)
      // })

      const { data } = await reqGetDepartments()
      // console.log(data)
      this.listData = data.depts
      // console.log(data)
      // 设置公司名称给公司插槽用
      this.company = { name: data.companyName, manager: '负责人', id: '' }
      // 将列表数据转换成树形数据
      this.departs = transListDataToTreeData(data.depts, '')

      this.loading = false
    },
    // 关闭弹出框
    // handleCloseFn() {
    //   this.showDialog = false
    // },
    // 显示弹出框
    showAddDialog(node) {
      // 1.让弹出框显示
      this.showDialog = true
      // 2.让添加node给父级inde.vue
      this.nodeData = node

      // 在父组件中调用子组件的方法
      // this.$refs.form.validate()
      // this.$refs.addDept.getEmplyeeList()
      // this.$refs.addDept.showTitle = '新增子部门'
    },
    showEditDialog(node) {
      // 1.让弹出框显示
      this.showDialog = true // 异步
      // node得到自己
      this.nodeData = node

      // this.$refs.addDept.showTitle = '编辑部门'

      this.$nextTick(() => {
        this.$refs.addDept.getDepartmentDetail()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
